<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\adminapi\controller\plugin;

/**
 * 插件市场登录状态管理器
 * 简单的单用户登录状态管理
 */
class LoginManager
{
    private $cacheFile;
    private $tokenExpire = 30 * 24 * 3600; // 30天过期

    public function __construct()
    {
        $cacheDir = runtime_path() . '/plugin_cache';
        if (!is_dir($cacheDir)) {
            mkdir($cacheDir, 0755, true);
        }
        $this->cacheFile = $cacheDir . '/market_login.json';
    }

    /**
     * 保存登录状态
     */
    public function saveLogin($email, $token, $userInfo)
    {
        $loginData = [
            'email'       => $email,
            'token'       => $token,
            'user_info'   => $userInfo,
            'login_time'  => time(),
            'expire_time' => time() + $this->tokenExpire
        ];

        file_put_contents($this->cacheFile, json_encode($loginData, JSON_PRETTY_PRINT));
        return $token;
    }

    /**
     * 获取登录状态
     */
    public function getLogin()
    {
        if (!file_exists($this->cacheFile)) {
            return null;
        }

        $loginData = json_decode(file_get_contents($this->cacheFile), true);
        if (!$loginData) {
            return null;
        }

        // 检查是否过期
        if (time() > $loginData['expire_time']) {
            $this->removeLogin();
            return null;
        }

        return $loginData;
    }

    /**
     * 检查是否已登录
     */
    public function isLoggedIn()
    {
        return $this->getLogin() !== null;
    }

    /**
     * 移除登录状态
     */
    public function removeLogin()
    {
        if (file_exists($this->cacheFile)) {
            unlink($this->cacheFile);
        }
    }

    /**
     * 刷新登录状态的过期时间
     */
    public function refreshLogin()
    {
        $loginData = $this->getLogin();
        if ($loginData) {
            $loginData['expire_time'] = time() + $this->tokenExpire;
            file_put_contents($this->cacheFile, json_encode($loginData, JSON_PRETTY_PRINT));
            return true;
        }
        return false;
    }

    /**
     * 验证token是否匹配
     */
    public function validateToken($token)
    {
        $loginData = $this->getLogin();
        return $loginData && $loginData['token'] === $token;
    }

    /**
     * 验证email是否匹配
     */
    public function validateEmail($email)
    {
        $loginData = $this->getLogin();
        return $loginData && $loginData['email'] === $email;
    }
}
